package edu.neu.swc.cloud.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.cloud.domain.Bide;
import edu.neu.swc.cloud.domain.Order;
import edu.neu.swc.cloud.domain.QueryInfo;
import edu.neu.swc.cloud.service.BideService;
import edu.neu.swc.cloud.service.OrderService;
import edu.neu.swc.cloud.service.PlanService;
import edu.neu.swc.cloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    ProductService productService;

    @Autowired
    BideService bideService;

    @Autowired
    PlanService planService;

    @RequestMapping("/orderList")
    public String orderList(QueryInfo queryInfo){
        int numbers = orderService.getOrderCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Order> orderList = orderService.findOrderByQuery("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",orderList);
        System.out.println("总条数："+numbers);
        System.out.println(orderList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/addOrder")
    public String addOrder(@RequestBody Order order){
        orderService.addOrder(order);
        return "success";
    }

    @RequestMapping("/getUpdate")
    public String getUpdate(int id){
        Order order = orderService.findOrdById(id);
        System.out.println("id:" + id);
        Map<String,Object> res = new HashMap<>();
        if(order.getOrd_state() == 0){
            res.put("flag","ok");
            res.put("data",order);
        }else{
            res.put("flag","error");
        }
        return JSON.toJSONString(res);
    }

    @RequestMapping("/editOrder")
    public String editOrder(@RequestBody Order order){
        orderService.updateOrder(order);
        return "success";
    }

    @RequestMapping("/deleteOrder")
    public String deleteOrder(int id){
        String flag = "error";
        Order order = orderService.findOrdById(id);
        if(order.getOrd_state() == 0){
            //订单未发布，可以进行删除
            orderService.deleteOrderById(id);
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/releaseOrder")
    public String releaseOrder(int id){
        Order order = orderService.findOrdById(id);
        String flag = "error";
        if(order.getOrd_state() == 0){
            orderService.releaseById(id);
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/isSelected")
    public String isSelected(int id){
        Order order = orderService.findOrdById(id);
        String flag = "error";
        if(order.getOrd_state() == 1){
            orderService.releaseById(id);
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/releasedOrderList")
    public String releasedOrderList(QueryInfo queryInfo){
        int numbers = orderService.getReleasedOrderCounts("%"+queryInfo.getQuery()+"%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Order> orderList = orderService.findReleasedOrderByQuery("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",orderList);
        System.out.println("总条数："+numbers);
        System.out.println(orderList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/isInBide")
    public String isInBide(QueryInfo queryInfo){
        Bide bide = bideService.findBideByOrdAndFacId(queryInfo.getFac(),queryInfo.getOrd());
        System.out.println(bide);
        String flag = "error";
        if(bide == null){
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/bideList")
    public String bideList(QueryInfo queryInfo){
        int numbers = bideService.getBideCounts(queryInfo.getOrd());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Bide> bideList = bideService.findBideByQuery(queryInfo.getOrd(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",bideList);
        System.out.println("总条数："+numbers);
        System.out.println(bideList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/hitOrder")
    public String hitOrder(QueryInfo queryInfo){
        orderService.hitOrder(queryInfo.getFac(),queryInfo.getOrd());
        return "success";
    }

    @RequestMapping("/stopPlanOrder")
    public String stopPlanOrder(int id){
        String flag = "error";
        Order order = orderService.findOrdById(id);
        if(order.getOrd_state() == 3){
            orderService.endPlanById(id);
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/transportOrder")
    public String transportOrder(int id){
        String flag = "error";
        Order order = orderService.findOrdById(id);
        if(order.getOrd_state() == 4){
            orderService.transportById(id);
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/receiveOrder")
    public String receiveOrder(int id){
        String flag = "error";
        Order order = orderService.findOrdById(id);
        if(order.getOrd_state() == 5){
            orderService.endById(id);

            flag = "success";
        }
        return flag;
    }
}
